using UnityEngine;

namespace Game {
	public sealed class EngineSound : MonoBehaviour {
		public ShipPhysics ShipPhysics;
		public AudioSource Sound;

		LerpHelper _soundLerper;

		private void Start() {
			_soundLerper = new LerpHelper(1.5f, 0f);
		}

		private void Update() {
			_soundLerper.TargetValue = Mathf.Abs(ShipPhysics.Thrust);
			_soundLerper.Update(Time.deltaTime);
			Sound.pitch = (_soundLerper.CurrentValue).Remap(0, 1, 0.75f, 1.35f);
			Sound.volume = (_soundLerper.CurrentValue.Remap(0, 1, 0f, 0.6f));
			if ( _soundLerper.CurrentValue > 0.01f && !Sound.isPlaying) {
				Sound.Play();
			} else if ( _soundLerper.CurrentValue < 0.01f && Sound.isPlaying) {
				Sound.Stop();
			}
		}
	}
}
